package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.Gender;
import cc.eddic.pratice.programmingparadigms.exam.MasterDataService;
import cc.eddic.pratice.programmingparadigms.exam.domain.Category;
import cc.eddic.pratice.programmingparadigms.exam.domain.Exam;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;
import cc.eddic.pratice.programmingparadigms.exam.domain.SubCategory;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

@Slf4j
public class MasterDataServiceWqjImpl implements MasterDataService {

    @Override//写MasterDataService里面的具体方法实现
    //泛型
    public List<Category> getCategories() {//将类型作为参数，可以理解为Category List
        //new是创建一个对象，也叫类的实例化
        Category software = new Category("软件开发基础");
        software.add(new SubCategory("基础"));
        software.add(new SubCategory("HTML"));
        software.add(new SubCategory("SQL"));
        software.add(new SubCategory("DOS"));
        software.add(new SubCategory("Linux"));
        software.add(new SubCategory("数据"));

        Category office = new Category("办公软件");
        office.add(new SubCategory("Word")).add(new SubCategory("Excel"));
//        office.add(new SubCategory("Word"));
//        office.add(new SubCategory("Excel"));

        Category coding = new Category("代码");

        return new ArrayList<Category>(Arrays.asList(software, office, coding));
    }

    @Override
    public List<Student> getStudents() {
        return new ArrayList<>(Arrays.asList(
                new Student("崔相元", Gender.MALE, "数据科学"),
                new Student("孙晨玮", Gender.MALE, "土木工程"),
                new Student("王巧家", Gender.MALE, "交通运输"),
                new Student("车晓满", Gender.MALE, "软件工程"),
                new Student("曾奕哲", Gender.MALE, "软件工程"),
                new Student("李馨", Gender.FEMALE, "软件工程"),
                new Student("李惠", Gender.FEMALE, "软件工程"),
                new Student("韩睿洁", Gender.FEMALE, "软件工程"),
                new Student("谭雨晴", Gender.FEMALE, "通信工程")
        ));
    }

    @Override
    public List<Exam> getExams() {
        List<Exam> examList = new ArrayList<>();
        Optional<Exam> optionalExam = find(examList, new Predicate<Exam>() {
            @Override
            public boolean test(Exam exam) {
                return "7-31 写代".equals(exam.getName());
            }
        });

        return new ArrayList<>(Arrays.asList(
                new Exam("7-12 开营考试"),
                new Exam("7-31 写代码"),
                new Exam("8-15 理论"),
                new Exam("8-25 答辩")));
    }

    @Override
    //optional 判断是否为null
    //List是传入的，Predicated是判断的
    public <T> Optional<T> find(List<T> genericList, Predicate<T> predicate) {
        //通用搜索设计
        for (T o : genericList){
            if (predicate.test(o)){
                return Optional.of(o);
            }
        }
        return Optional.empty();
    }
}

